package com.heima.common.exception;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 方法增强注解
 */
@RestControllerAdvice
public class ExceptionCatch {
    /**
     * ExceptionHandler全局异常，所有向上抛的异常都会被其捕获名返回
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResponseResult exceptionHandler(Exception e){
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    /**
     * 自定义异常捕获器
     * @param e
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public ResponseResult customException(Exception e){
        if(e instanceof CustomException){
            return ((CustomException) e).getResponseResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }


}
